/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Servers.java
 *
 * Created on May 12, 2011, 2:30:56 AM
 */

package View;

import DAO.ServerDAO;
import filetrans.Client;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import model.ServerInfo;

/**
 *
 * @author Oscar
 */
public class Servers extends javax.swing.JFrame {

    ServerDAO serverDao;
    Login Login;
    DefaultTableModel model;

    /** Creates new form Servers */
    public Servers(Login login, ServerDAO dao) {
        
        initComponents();

        this.Login = login;

        this.serverDao = dao;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        loadServers();

        this.setVisible(true);
    }

    private void loadServers() {

        model = new DefaultTableModel();
        model.setColumnIdentifiers(new String[] {"Ubicacion", "IP"});

        // Lleno la tabla
        for (ServerInfo myServer : serverDao.getAll())
        {
            model.addRow(new String[] {myServer.getName(), myServer.getIp()});
        }

        this.ServersTable.setModel(model);

        this.ServersTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        ServersTable = new javax.swing.JTable();
        AddServerButton = new javax.swing.JButton();
        RemoveServerButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        ServersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ubication", "IP"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(ServersTable);

        AddServerButton.setText("+");
        AddServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddServerButtonActionPerformed(evt);
            }
        });

        RemoveServerButton.setText("-");
        RemoveServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveServerButtonActionPerformed(evt);
            }
        });

        jButton4.setText("Guardar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(AddServerButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(RemoveServerButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 191, Short.MAX_VALUE)
                        .add(jButton4))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 344, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton4)
                    .add(AddServerButton)
                    .add(RemoveServerButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

        this.setVisible(false);

        this.Login.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void AddServerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddServerButtonActionPerformed
        
        model.addRow(new String[] {"X", "X"});
    }//GEN-LAST:event_AddServerButtonActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        
        boolean continuar = false;

        for (int i = 0; i < this.ServersTable.getModel().getRowCount(); i++) {

            String ip = (String) this.ServersTable
                                       .getModel()
                                       .getValueAt(i, 1);

            continuar = Client.validateIP(ip);


        }

        if (continuar) {

            serverDao.Servers.removeAll(serverDao.Servers);

            for (int i = 0; i < this.ServersTable.getModel().getRowCount(); i++) {

                String name = (String) this.ServersTable
                                           .getModel()
                                           .getValueAt(i, 0);

                String ip = (String) this.ServersTable
                                           .getModel()
                                           .getValueAt(i, 1);

                serverDao.Servers.add(new ServerInfo(name, ip));

            }

            serverDao.commit();

            this.dispose();
            this.Login.setVisible(true);
        }
        else {
            
            JOptionPane.showMessageDialog(this,
                "Una de sus IP no es correcta",
                "IP error",
                JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void RemoveServerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveServerButtonActionPerformed

        int row = this.ServersTable.getSelectedRow();
       ((DefaultTableModel)this.ServersTable.getModel()).removeRow(row);

    }//GEN-LAST:event_RemoveServerButtonActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddServerButton;
    private javax.swing.JButton RemoveServerButton;
    private javax.swing.JTable ServersTable;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
